import Vue from "vue";
import VueRouter from "vue-router";
import menu from "@/router/menu";
import notFound from "@/views/NotFound.vue";
// import Home from '../views/Dashboard/Home.vue'

// 解决重复点击菜单报错问题;
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: () => import("@/views/Login/index.vue"),
    meta: { title: "登录", name: "login" }
  },
  {
    path: "/error",
    component: () => import("@/views/Error/index.vue"),
    meta: { title: "未授权", name: "error" }
  },
  {
    path: "/entering",
    component: () => import("@/views/Error/entering.vue"),
    meta: { title: "加载中", name: "error" }
  },
  ...menu,
  {
    path: "*",
    component: notFound,
    meta: {
      auth: false
    }
  }
];

const router = new VueRouter({
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path === "/entering" || to.path === "/error") return next();
  if (!sessionStorage.getItem("token")) {
    next({ path: "/entering" });
    return;
  }
  next();
});

export default router;
